package sy.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.IotEntranceURIProjectMappingDaoI;
import sy.dao.IotVideoURIProjectMappingDaoI;
import sy.model.IotEntranceURI;
import sy.model.IotEntranceURIProjectMapping;
import sy.model.IotVideoURIProjectMapping;
@Repository("iotVideoURIProjectMappingDao")
public class IotVideoURIProjectMappingDaoImpl implements IotVideoURIProjectMappingDaoI{
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}

	@Override
	public void save(IotVideoURIProjectMapping iotVideoURIProjectMapping) {
		// TODO Auto-generated method stub
		this.getCurrentSession().save(iotVideoURIProjectMapping);
	}

	@Override
	public void update(IotVideoURIProjectMapping iotVideoURIProjectMapping) {
		// TODO Auto-generated method stub
		this.getCurrentSession().saveOrUpdate(iotVideoURIProjectMapping);
	}

	@Override
	public void delete(List<Integer> ids) {
		// TODO Auto-generated method stub
		String hql = "delete from IotVideoURIProjectMapping where id in :ids";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setParameterList("ids", ids);
		query.executeUpdate();
	}

	@Override
	public List<IotVideoURIProjectMapping> list() {
		// TODO Auto-generated method stub
		return this.getCurrentSession().createQuery("from IotVideoURIProjectMapping").list();
	}

	@Override
	public List<IotVideoURIProjectMapping> listIotVideoURIProjectMappingByKey(
			String key) {
		// TODO Auto-generated method stub
		String hql = "from IotVideoURIProjectMapping where iotVideoURI.id like :key or sysProject.id like :key";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("key", "%"+key+"%");
		return query.list();
	}

	@Override
	public IotVideoURIProjectMapping getIotVideoURIProjectMappingById(
			Integer id) {
		// TODO Auto-generated method stub
		String hql = "from IotVideoURIProjectMapping where id = :id";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setParameter("id", id);
		List<IotVideoURIProjectMapping> list = query.list();
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}
}
